package com.eastcode.dictionary.action;

import java.util.List;

import com.eastcode.base.action.BaseAction;
import com.eastcode.base.util.DateUtil;
import com.eastcode.dictionary.entity.Dictionary;
import com.eastcode.dictionary.service.DictionaryService;
import com.eastcode.dictionary.view.DictionaryView;
import com.eastcode.mobile.server.util.ServerLocator;

public class DictionaryAction extends BaseAction {

	private static final long serialVersionUID = -5391649022188869L;

	private DictionaryService dictionaryService = ServerLocator.getDictionaryService();

	public void execute() {
		prepare(this);
	}

	/**
	 * 查询数据
	 * 
	 */
	public void search() {
		DictionaryView view = parseRequest(DictionaryView.class, "view.");

		List<Dictionary> result = dictionaryService.searchList(Dictionary.class, view);
		view.setResult(result);

		request.setAttribute("view", view);

		forward("/template/server/dictionary/list.ftl");
	}

	/**
	 * 插入数据
	 * 
	 */
	public void insert() {
		DictionaryView view = parseRequest(DictionaryView.class, "view.");
		request.setAttribute("view", view);

		forward("/template/server/dictionary/input.ftl");
	}

	/**
	 * 保存数据：新增或修改
	 * 
	 * @return
	 */
	public void save() {
		Dictionary dictionary = parseRequest(Dictionary.class, "");

		long id = Long.parseLong(request.getParameter("view.id"));

		// 处理实体的Key值
		if (id != 0) {
			Dictionary old = dictionaryService.findObjectByKey(Dictionary.class, id);
			dictionary.setId(old.getId());
			dictionary.setUpdateTime(DateUtil.getToday());
		} else {
			dictionary.setCreateTime(DateUtil.getToday());
		}

		dictionaryService.save(dictionary);

		setResult(dictionary);

		DictionaryView view = parseRequest(DictionaryView.class, "view.");

		request.setAttribute("view", view);

		forward("/template/server/dictionary/detail.ftl");
	}

	/**
	 * 禁用数据
	 * 
	 * @return
	 */
	public void disable() {
		Dictionary dictionary = parseRequest(Dictionary.class, "");

		long id = Long.parseLong(request.getParameter("view.id"));
		String enable = request.getParameter("view.enable");

		dictionary = dictionaryService.findObjectByKey(Dictionary.class, id);

		// 更新数据
		dictionary.setState(enable);
		dictionary.setUpdateTime(DateUtil.getToday());

		dictionaryService.save(dictionary);

		search();
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public void delete() {
		long id = Long.parseLong(request.getParameter("view.id"));
		dictionaryService.deleteObjectByKey(Dictionary.class, id);
		search();
	}

	/**
	 * 更新数据
	 * 
	 */
	public void update() {

		long id = Long.parseLong(request.getParameter("view.id"));
		Dictionary dictionary = dictionaryService.findObjectByKey(Dictionary.class, id);
		setResult(dictionary);

		DictionaryView view = parseRequest(DictionaryView.class, "view.");
		view.setId(id);
		request.setAttribute("view", view);

		forward("/template/server/dictionary/input.ftl");
	}

	/**
	 * 查看详情
	 * 
	 */
	public void detail() {
		long id = Long.parseLong(request.getParameter("view.id"));
		Dictionary dictionary = dictionaryService.findObjectByKey(Dictionary.class, id);
		setResult(dictionary);

		DictionaryView view = parseRequest(DictionaryView.class, "view.");
		view.setId(id);
		request.setAttribute("view", view);

		forward("/template/server/dictionary/detail.ftl");
	}
}